package cn.itcast.controller;

import cn.itcast.pojo.*;
import cn.itcast.service.EmpService;
import com.aliyuncs.exceptions.ClientException;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    //分页查找
    @GetMapping
    public Result findByCondition(QuaryParma quaryParma) {
        PageInfo<Emp> bc = empService.findByCondition(quaryParma);
        return bc != null ? Result.success(bc) : Result.erro("没有查询到数据");
    }

    //添加员工
    @PostMapping
    public Result add(@RequestBody Emp emp) {
        boolean result = empService.add(emp);

        return result ? Result.success("success") : Result.erro("添加失败");
    }

    //根据id可以批量删除
    @DeleteMapping
    public Result deleteById(int[] ids) {
        log.info("id{}", ids);
        boolean result = empService.deleteById(ids);

        return result ? Result.success("删除成功") : Result.erro("删除失败");
    }

    //根据id查找员工信息
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Emp result = empService.findById(id);

        return result != null ? Result.success(result) : Result.erro("查询失败");
    }

    //修改员工信息
    @PutMapping
    public Result update(@RequestBody Emp emp) throws ClientException {
        boolean result = empService.update(emp);

        return result ? Result.success("修改成功") : Result.erro("修改失败");
    }

    //查找全部员工信息
    @GetMapping("/list")
    public Result findAll() {
        List<Emp> result = empService.findAll();

        return result != null ? Result.success(result) : Result.erro("查询失败");
    }

    //批量查询员工
    @GetMapping("/ids")
    public Result findByIds(int[] ids) {
        System.out.println("接收到的ids参数: " + Arrays.toString(ids));
        List<Emp> byIds = empService.findByIds(ids);
        return byIds != null ? Result.success(byIds) : Result.erro("查询失败");
    }

}
